<template>
  <div>
    <el-form
      ref="formBizExam"
      :model="formBizExam"
      label-width="75px"
      :size="defaultFormItemSize"
      label-position="right"
      @submit.native.prevent
    >
      <filter-box :item-width="350" @search="refreshFormBizExam(true)" @reset="onReset">
        <el-form-item label-width="150px" label="请选择评价问卷" prop="formFilter.surveyId">
          <el-select
            class="filter-item"
            v-model="formBizExam.formFilter.surveyId"
            :clearable="true"
            placeholder=""
            filterable
          >
            <el-option
              v-for="item in formBizExam.surveyList"
              :key="item.id"
              :value="item.id"
              :label="item.surveyName"
            />
          </el-select>
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      class="page-table"
      :data="formBizExam.SysUser.impl.dataList"
      :size="defaultFormItemSize"
      @sort-change="formBizExam.SysUser.impl.onSortChange"
      @refresh="refreshFormBizExam(true)"
      :seq-config="{
        startIndex: (formBizExam.SysUser.impl.currentPage - 1) * formBizExam.SysUser.impl.pageSize
      }"
    >
      <vxe-column title="序号" type="seq" width="50px" />
      <vxe-column title="评价问卷名称" field="surveyName" />
      <vxe-column title="姓名" field="userName" />
      <vxe-column title="总分" field="totalScore" />
      <vxe-column title="排名" field="rank" />
      <vxe-column title="操作" fixed="right" width="230px">
        <template slot-scope="scope">
          <el-button
            class="btn-table-edit"
            type="text"
            :size="defaultFormItemSize"
            @click="detail(scope.row)"
          >
            详情
          </el-button>
        </template>
      </vxe-column>

      <el-row slot="pagination" type="flex" justify="end" style="margin-top: 16px">
        <el-pagination
          :total="formBizExam.SysUser.impl.totalCount"
          :current-page="formBizExam.SysUser.impl.currentPage"
          :page-size="formBizExam.SysUser.impl.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, prev, pager, next, sizes"
          @current-change="formBizExam.SysUser.impl.onCurrentPageChange"
          @size-change="formBizExam.SysUser.impl.onPageSizeChange"
        >
        </el-pagination>
      </el-row>
    </table-box>
  </div>
</template>

<script>
/* eslint-disable-next-line */
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js';
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins';
/* eslint-disable-next-line */
import {BizEvluateRecordController, BizEvluateSurveyController} from '@/api';
import formShowEvluateDetail from '@/views/biz/formShowEvluateDetail';

export default {
  name: 'formBizEvluateRank',
  props: {},
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data() {
    return {
      deptIdPath: [],
      formBizExam: {
        formFilter: {
          surveyId: ''
        },
        formFilterCopy: {
          surveyId: ''
        },
        SysUser: {
          impl: new TableWidget(
            this.loadBizExamData,
            this.loadBizExamVerify,
            true,
            false,
            'createTime',
            1
          )
        },
        isInit: false,
        surveyList: []
      }
    };
  },
  methods: {
    onReset() {
      this.$refs.formBizExam.resetFields();
      this.refreshFormBizExam(true);
    },
    loadBizExamData(params) {
      params.filter = {
        surveyId: this.formBizExam.formFilter.surveyId
      };
      return new Promise((resolve, reject) => {
        BizEvluateRecordController.rankList(this, params)
          .then(res => {
            resolve({
              dataList: res.data.dataList,
              totalCount: res.data.totalCount
            });
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadBizExamVerify() {
      this.formBizExam.formFilterCopy.surveyId = this.formBizExam.formFilter.surveyId;
      return true;
    },
    refreshFormBizExam(reloadData = false) {
      // 重新获取数据组件的数据
      if (reloadData) {
        this.formBizExam.SysUser.impl.refreshTable(true, 1);
      } else {
        this.formBizExam.SysUser.impl.refreshTable();
      }
      this.formBizExam.isInit = true;
    },
    onResume() {
      this.refreshFormBizExam();
    },
    initFormData() {},
    formInit() {
      this.initFormData();
      this.refreshFormBizExam();
    },
    loadPaperListDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {};
        BizEvluateSurveyController.listSurvey(this, params)
          .then(res => {
            this.formBizExam.surveyList = res.data;
            this.getCurrentSurvey();
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    getCurrentSurvey() {
      return new Promise((resolve, reject) => {
        let params = {};
        BizEvluateSurveyController.getPublishSurvey(this, params)
          .then(res => {
            if (res && res.data) {
              this.formBizExam.formFilter.surveyId = res.data.id;
            }
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    detail(row) {
      let params = {
        moduleType: 5,
        selectSurveyId: row.surveyId,
        selectUserId: row.userId
      };
      BizEvluateRecordController.exist(this, params)
        .then(res => {
          if (res.data) {
            this.$dialog
              .show(
                '评价详情',
                formShowEvluateDetail,
                {
                  area: '1200px'
                },
                {
                  rowData: res.data
                }
              )
              .then(res => {
                this.refreshFormBizExam();
              })
              .catch(e => {});
          }
        })
        .catch(e => {
          console.error(e);
        });
    }
  },
  created() {
    this.loadPaperListDropdownList();
    this.formInit();
  }
};
</script>
